require( '$d/css/common.less' );
require( './style.less' );
define( [
	'jquery',
	'require',
	'header',
	'message',
	'api',
	'bigwe_utils',
	'dateRange',
	'bootstrap',
	'combiner',
	'artTemplate',
	'nav'
], function( $, require ) {
	
	var Dr = require( 'dateRange' );
	var msg = require( 'message' );
	var api = require( 'api' );
	var bu = require( 'bigwe_utils' );
	var at = require( 'artTemplate' );

	var thisApi = api.GATHER;
	var k_Milliseconds = Date.now() - 86400000 * 30;
	var sd = new Date( k_Milliseconds ).format( 'yyyy-MM-dd' );
	var ed = new Date( Date.now() ).format( 'yyyy-MM-dd' );
	// var platf=null;
	// var linkSplit=bu.getUrlParam( 'searchurl' ).split('.');
	// if(linkSplit[1]=='weixin'){
	// 	platf='mp.weixin.qq.com';
	// }else{
	// 	platf='toutiao.com';
	// }
	var history = {
		options: {
			//url: bu.getUrlParam( 'searchurl' ),
			//platform:platf,
			id:bu.getUrlParam( 'id' ),
			key: '',
			p: 1,
			l: 10,
			st: sd,
			et: ed
		},
		doSearch: function( options, isreloadPage ) {

			var pageParam = {};
			var $tbody = $( '.tbl_history tbody' ).empty();

			if ( typeof options === 'string' ) {
				pageParam.currentPage = $( '#pagewrapper .current_page' ).data( 'currentPage' );
				options = {};
			}
			$( '#pagewrapper' ).hide();

			return $.ajax( {
				data: $.extend( this.options, options ),
				url: thisApi.HISTORY_ARTS,
				type: 'post',
				success: function( data ) {

					$( '#pagewrapper' ).show();
					data = typeof data === 'string' ? $.parseJSON( data ) : data;
					if ( data.ret === 0 ) {
						if ( data.data.arts && data.data.arts.length ) {
							for(var i in data.data.arts){
								var _title=data.data.arts[i].title;
								if(_title.length>20){
									data.data.arts[i].title=_title.substring(0,20)+'...';
								}
								
							}
							$tbody.append( at( 'history_html_item', {
								list: data.data.arts
							} ) ).find( '[data-toggle="tooltip"]' ).tooltip();
						} else {
							$tbody.html( '<tr><td colspan="4" style="padding: 100px 0;" align="center">暂无数据</td></tr>' );
						}

						if ( isreloadPage ) {
							this.paging( $.extend( {
								total: data.data.count
							}, pageParam ) );
						}
						$( '#history_count' ).text( data.data.count || 0 );
					}
				}.bind( this )

			} );
		},
		paging: function( total ) {
			return $( '#pagewrapper' ).bwPagination( $.extend( {
				autoVisibility: !0,
				callback: function( _index ) {
					$( 'body' ).animate( {
						scrollTop: $( '.main .main_left' ).offset().top
					}, .2e3 );
					history.doSearch( {
						p: _index
					} );
				}.bind( this )
			}, total ) );
		}
	};

	history.doSearch( null, true );

	new Dr( 'date-fromto-text', {
		isTodayValid: true,
		startDate: sd,
		endDate: ed,
		isSingleDay: false, // 是否单选天
		shortOpr: false, // 是否只选一天
		defaultText: ' 至 ',
		inputTrigger: 'date-fromto',
		theme: 'ta',
		stopToday: true,
		//isTodayValid : false,
		success: function( obj ) {
			history.doSearch( {
				p: 1,
				st: obj.startDate,
				et: obj.endDate
			}, true );
		}
	} );


	// 收藏
	$( document ).on( 'click', '.js_docollect', function() {
		var cd = $( this ).parent().data( 'id' );
		$.ajax( {
			url: api.COLLECT_ART.GET_COLLECT_ART,
			type: 'post',
			data: {
				rt: 'collectArt',
				cd: cd
			},
			success: function( data ) {
				data = typeof data == 'string' ? $.parseJSON( data ) : data;
				if ( data.ret === 0 ) {
					$( this ).addClass( 'golden' );
					msg.show( '收藏成功！', msg.showTime );
				} else {
					msg.showWarning( data.msg, msg.showTime );
				}
			}.bind( this )
		} );
	} ).on( 'click', '.do_reset', function() {
		$( '.search_title' ).val( '' );
		$( '#date-fromto-text' ).text( sd + ' 至 ' + ed );
		history.doSearch( {
			key: '',
			p: 1,
			st: sd,
			et: ed
		}, true );
	} ).on( 'click', '.do_search', function() {
		history.doSearch( {
			key: $( '.search_title' ).val(),
		}, true );
	} );


} );